<?PHP

require_once __DIR__.DIRECTORY_SEPARATOR.'functions.php';


function getContent($data)
{
    switch ($data['MsgType']) {
        case 'text':
            $content .= "\t\t".$data['Content'];
            break;
        case 'image':
            $content .= "\t\turl: ".$data['PicUrl'].PHP_EOL;
            $content .= "\t\tid: ".$data['MediaId'];
            break;
        case 'voice':
            $content = "\t\t格式: ".$data['Format'].PHP_EOL;
            $content .= "\t\tid: ".$data['MediaId'];
            if (isset($data['Recognition']))
                $content .= PHP_EOL."\t\t识别文本: ".$data['Recognition'];
            break;
        case 'video':
        case 'shortvideo':
            $content = "\t\tid: ".$data['MediaId'].PHP_EOL;
            $content .= "\t\t缩略图: ".$data['ThumbMediaId'];
            break;
        case 'location':
            $content = "\t\t纬度: ".$data['Location_X'].PHP_EOL;
            $content .= "\t\t经度: ".$data['Location_Y'].PHP_EOL;
            $content .= "\t\t缩放大小: ".$data['Scale'].PHP_EOL;
            $content .= "\t\t位置信息: ".$data['Label'];
            break;
        case 'link':
            $content = "\t\t标题: ".$data['Title'].PHP_EOL;
            $content .= "\t\t简介: ".$data['Description'].PHP_EOL;
            $content .= "\t\t链接: ".$data['Url'];
            break;
        case 'event':
            $content = "\t\t事件类型: ".$data['Event'].PHP_EOL;
            $content .= "\t\t事件KEY: ".$data['EventKey'];
            if (isset($data['Ticket']))
                $content .= PHP_EOL."\t\tTicket: ".$data['Ticket'];
            if (isset($data['Latitude']))
                $content .= PHP_EOL."\t\t纬度: ".$data['Latitude'];
            if (isset($data['Longitude']))
                $content .= PHP_EOL."\t\t经度: ".$data['Longitude'];
            if (isset($data['Precision']))
                $content .= PHP_EOL."\t\t精度: ".$data['Precision'];
            break;
        default:
            $content = "????? 你想干吗 ?????";
            break;
    }

    return $content;
}


function main()
{
    $http_method = $_SERVER['REQUEST_METHOD'];

    //微信修改服务器配置验证服务器token
    if ($http_method === 'GET' && isset($_GET['echostr'])) {
        checkSignature(TRUE);
    } else if ($http_method == 'POST' && checkSignature()) {
        $post = DEBUG ? parseXML(kyo_log()): getPost(TRUE);

        $content = "你刚才给我发的信息:".PHP_EOL;
        $content .= "\t类型: ".MSG_TYPE_TEXT[$post['MsgType']].PHP_EOL;
        $content .= "\t时间: ".date('Y-m-d H:i:s', $post['CreateTime']).PHP_EOL;
        $content .= "=== 以下为信息内容 ===".PHP_EOL;
        $content .= getContent($post);

        $xml = '<xml>';
        $xml .= '<ToUserName><![CDATA['.$post['FromUserName'].']]></ToUserName>';
        $xml .= '<FromUserName><![CDATA['.$post['ToUserName'].']]></FromUserName>';
        $xml .= '<CreateTime>'.$post['CreateTime'].'</CreateTime>';
        $xml .= '<MsgType><![CDATA[text]]></MsgType>';
        $xml .= '<Content><![CDATA['.$content.']]></Content>';
        $xml .= '</xml>';

        echo $xml;
    }
}

main();

